<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./js/art-template.js"></script>
    <script src="./js/jquery.js"></script>
    <style>
        .news {
            border: 1px solid #ccc;
            padding: 10px;
            display: flex;
            margin-bottom: 10px;
        }

        .tags span {
            border-radius: 16px;
            background-color: #ccc;
            padding: 2px 10px;
            margin: 0px 10px;
        }

        .right {
            margin-left: 20px;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            flex: 1;
        }

        .footer {
            display: flex;
            justify-content: space-between;
        }
    </style>
</head>

<body>
    <div id="news-list"></div>
    <!-- 定义模板 -->
    <script type="text/html" id="tpl-news">
        {{each data}}
        <div class="news">
            <img src="{{'http://www.liulongbin.top:3006'+$value.img}}">
            <div class="right">
                <h1>{{$value.title}}</h1>
                <div class="tags">
                    {{each $value.tags}}
                    <span>{{$value}}</span>
                    {{/each}}
                </div>
                <!-- 媒体 -->
                <div class="footer">
                    <div>
                        <span>{{$value.source}}</span>
                        <span>{{$value.time}}</span>
                    </div>
                    <span>评论数：{{$value.cmtcount}}</span>
                </div>
            </div>
        </div>
        {{/each}}
    </script>
    <!-- 数据 -->
    <script>
        //获取到所有的新闻
        $.ajax({
            type: 'get',
            url: 'http://www.liulongbin.top:3006/api/news',
            success: function (res) {
                if (res.status !== 200) {
                    alert('获取失败')
                }

                for (var i = 0; i < res.data.length; i++) {
                    res.data[i].tags = res.data[i].tags.split(',');
                    // console.log(res.data.tags);
                }
                var htmlStr = template('tpl-news', res);
                $('#news-list').html(htmlStr);
            }
        })
    </script>
</body>

</html>